<?php

namespace app\api\model;
use think\Model;
use think\Config;

class User extends Model
{
    protected $table = 'jk_users';

    //  //头像拼接域名  之前的做法，暂时弃用
    // public function getUserIconUrlAttr($value)
    // {
    // 	if(empty($value)){
    // 		$res = Config::get('pic_url.default');
    // 	} else{
    // 		$res = Config::get('pic_url.user') . $value;
    // 	}

    //     return $res;
    // }

      //通过user_id 找出用户的头像
    public function getUserIconUrlAttr($value)
    {
        // $res = \think\Db::table('jk_user_informations')->where('user_id', $value)->field('praise, commonly, negative_comment')->find();
        $res = \think\Db::table('jk_users')->alias('u')
            ->where('u.user_id', $value)
            ->join('jk_user_informations m', 'm.user_id = u.user_id', 'LEFT')
            ->join('jk_user_third t', 't.openid = u.wechat_openid', 'LEFT')
            ->field('m.user_icon_url, t.avatar')
            ->find();
        if(!empty($res['avatar'])){
            $info = $res['avatar'];
        } elseif(!empty($res['user_icon_url'])){
            $info =Config::get('web.pic_url')['user'] . $res['user_icon_url'];
        } else{
            $info = Config::get('web.pic_url')['default'];
        }

        return $info;
        // return $res;
    }

    //等级图片
    public function getLevelPicAttr($value)
    {
        $res = Config::get('web.pic_url')['level'] . $value;

        return $res;
    }

    //输出认证类型
    public function getTypeAttr($value)
    {
        $status = [
            '1'=>'个人',
            '2'=>'企业', 
            '3'=>'未认证', 
        ];

        return $status[$value];
    }

    //根据id查出用户信誉度
    public function getXinyuAttr($value)
    {
        $res = \think\Db::table('jk_user_informations')->where('user_id', $value)->field('praise, commonly, negative_comment')->find();
        $info = praise($res['praise'], $res['commonly'], $res['negative_comment']);

        return $info;
    }

    //友好显示时间
    public function getCreateAtAttr($value)
    {
        $res = friendlyDate($value);

        return $res;
    }


}
